package yxy.game.pm2.command.stairway;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.StairwayMessage;
import yxy.game.pm2.module.stairway.StairwayHandle;

/** 天梯争雄 天梯挑战记录 */
@MessageType(proto = Command.StairwayRecord, classtype = StairwayMessage.StairwayRecordRequest.class)
public class CMD_StairwayRecord extends Command<StairwayMessage.StairwayRecordRequest> {

	@Override
	public void execute(MessageSource messageSource, StairwayMessage.StairwayRecordRequest request) {
		String playerCode = messageSource.getTag();

		StairwayMessage.StairwayRecordResponse.Builder response = StairwayMessage.StairwayRecordResponse.newBuilder();

		try {
			Result result = new StairwayHandle().record(playerCode, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, StairwayMessage.StairwayRecordResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}